



<template>
  <div>
      <div style="display: flex"> 
      <ht-upload
        v-if="busType != 'quality'"
        ref="myUpload"
        class="upload-demo"
        dbKey="Trd"
        busField="workReport"
        :busType="busType"
        :busKey="busKey + ''"
        :ftpKey="ftp"
        v-model="fileList"
        :show-file-list="false"
        :before-upload="beforUpload"
        :on-success="afterUpload"
      >
        <a-button type="link"> 上传 </a-button>
      </ht-upload>
      <a-button type="link" @click="handleDownload()"> 下载 </a-button>
      <a-button v-if="busType != 'quality'" type="link" @click="removeAttachRow()"> 删除 </a-button>
    </div>
    <a-table
      :columns="columns"
      :data-source="attachs"
      bordered
      :pagination="false"
      :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
    >
      <!-- <span slot="uploadTimeRender" slot-scope="text, record">
        <span v-if="record.createTime && record.createTime != '1900-01-01 00:00:00'">
          {{ record.uploadTimeRender }}
        </span>
      </span> -->
      <!-- <span slot="index" slot-scope="text, record, index">
        {{ index + 1 }}
      </span> -->
    </a-table>
  </div>
</template>

<script>
import Vue from 'vue'
import { getAction, getActionAsync, postAction } from '@/api/manage'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import '@/config/upload.install.js'
export default {
  props: {
    busType: {
      type: String,
      default: '',
    },
    busKey: {
      type: String,
      default: '',
    },
    ftp: {
      type: String,
      default: 'ftp',
    },
    logBusKey: {
      type: String,
      default: '0',
    },
  },
  data() {
    return {
      urls: {
        attachs: '/hte/contract/attachList',
        attachSave: '/hte/contract/attachSave',
        attachDelete: '/hte/contract/attachDelete',
      },
      attachs: [],
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'index' },
        },
        {
          title: '文件名称',
          dataIndex: 'fileName',
          key: 'fileName',
        },
        {
          title: '文件大小',
          dataIndex: 'fileSize',
          key: 'fileSize',
        },
        {
          title: '上传人',
          dataIndex: 'uploaderName',
          key: 'uploaderName',
        },
        ,
        {
          title: '上传时间',
          dataIndex: 'uploadTime',
          key: 'uploadTime',
          scopedSlots: { customRender: 'uploadTimeRender' },
        },
      ],
      currentSelection: [],
      selectedRowKeys: [],
      fileList: [],
    }
  },
  mounted() {
    this.fetchAttach()
  },
  methods: {
    fetchAttach() {
      if (this.busType && this.busKey) {
        getAction(this.urls.attachs + '/' + this.busType + '/' + this.busKey + '/' + this.logBusKey, {}).then((res) => {
          if (res.success) {
            this.attachs = res.result
            if (!this.attachs || this.attachs.length == 0) {
              this.attachs = []
            }
          }
        }) 
      }
    },
    handleDownload() {
      if (this.currentSelection.length != 1) {
        return this.$messageWorkReport.warning('请选择一条记录下载')
      }
      let record = this.currentSelection[0]
      if (record.id) {
        const token = Vue.ls.get(ACCESS_TOKEN)
        window.open(
          process.env.VUE_APP_PS_API_BASE_URL +
            '/download/'+
            record.id
        )
      }
    },
    beforUpload() {
      if (this.currentSelection.length > 1) {
        this.$messageWorkReport.warning('请选择一条记录上传')
        return false
      }
      return true
    },
    afterUpload(response) {
      if (response && response.length > 0) {
        postAction(this.urls.attachSave + '/' + this.logBusKey, response[0]).then((res) => {
          if (res.success) {
            this.fetchAttach()
          } else {
            this.$messageWorkReport.error(res.message)
          }
        })
      }
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.currentSelection = selectedRows
    },
    removeAttachRow() {
      this.currentSelection.forEach((record) => {
        if (record.id) {
          getActionAsync(this.urls.attachDelete + '/' + record.id + '/' + this.logBusKey, {}).then((res) => {
            if (res.success) {
              this.$messageWorkReport.success('删除成功')
            } else {
              this.$messageWorkReport.error(res.message)
            }
          })
        }
      })
      this.fetchAttach()
      this.currentSelection = []
    },
  },
  watch: {
    busType(newV, oldV) {
      this.fetchAttach()
    },
    busKey(newV, oldV) {
      this.fetchAttach()
    },
  },
}
</script>

<style scoped>
/deep/.ant-col .ant-form-item-label {
  min-width: 135px;
  line-height: 40px;
  display: inline-block;
  overflow: hidden;
  line-height: 39.9999px;
  white-space: nowrap;
  text-align: right;
  vertical-align: middle;
}
/deep/.ant-row .ant-form-item {
  display: flex;
}
/deep/.ant-col .ant-form-item-control-wrapper {
  width: 100%;
}
/deep/.anticon .anticon-plus-circle {
  display: flex;
  align-items: center;
}
/deep/.ant-calendar-picker {
  width: 100%;
}
</style>  